<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="artDialog/skins/default.css"
	type="text/css"></link>
<link href="css/select.css" rel="stylesheet" type="text/css" />
<title>DEMO</title>
<script type="text/javascript" src="js/apiv1.3.min.js"></script>
<!--script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script-->
<link rel="stylesheet" type="text/css" href="bmap.css" />
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/select-ui.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/jquery.artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.plugins.min.js"></script>
<script type="text/javascript" src="js/util.js"></script>

</head>
<body>
	<div class="place">
		<span>位置：</span>
		<ul class="placeul">
			<li><a href="#">首页</a></li>
			<li><a href="#">消缺任务管理</a></li>
			<li><a href="#">消缺任务审批</a></li>
		</ul>
		<span style="color: FF6633;">左键塔杆确认右键保存</span>
	</div>

	<div
		style="width:100%;height:95%;border:1px solid gray;position:absolute;"
		id="container"></div>

</body>
</html>
<script type="text/javascript">
	var selectPole = [];
	var selectPoleId ="";
	
	
	
	
	var mapOptions = {
		mapType : BMAP_NORMAL_MAP,
		maxZoom : 10, //地图最大层级
		minZoom : 5
	};
	
	// 百度地图API功能
	var map = new BMap.Map("container", mapOptions); 		// 创建Map实例
	var initPoint = new BMap.Point(116.404, 39.915); // 创建点坐标
	map.centerAndZoom(initPoint, 8);  // 初始化地图,设置中心点坐标和地图级别。
	//添加地图类型控件
	map.addControl(new BMap.MapTypeControl({
		mapTypes : [ BMAP_NORMAL_MAP, BMAP_HYBRID_MAP ]
	}));
	map.setCurrentCity("北京");			 // 设置地图显示的城市 此项是必须设置的
	map.enableScrollWheelZoom(true);    //开启鼠标滚轮缩放
	map.enableKeyboard(); 				// 启用键盘操作。  
	map.enableContinuousZoom();        //启用连续缩放效果



	// ----- control -----
	map.addControl(new BMap.NavigationControl()); //地图平移缩放控件
	map.addControl(new BMap.ScaleControl()); //显示比例尺在右下角
	
	var overView = new BMap.OverviewMapControl();
	var overViewOpen = new BMap.OverviewMapControl({isOpen:true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT});
	map.addControl(overView);          //添加默认缩略地图控件
	map.addControl(overViewOpen);      //右下角，打开

	
	
	/* ----自定义控件---- */
	// 定义一个控件类,即function
	function ZoomControl(){
	  // 默认停靠位置和偏移量
	  this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
	  this.defaultOffset = new BMap.Size(75, 20);
	}
	
	function ZoomControl2(){
	  // 默认停靠位置和偏移量
	  this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
	  this.defaultOffset = new BMap.Size(135, 20);
	}
	
	
	// 通过JavaScript的prototype属性继承于BMap.Control
	ZoomControl.prototype = new BMap.Control();
	ZoomControl2.prototype=new BMap.Control();

	// 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
	// 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
	ZoomControl.prototype.initialize = function(map){
	  // 创建一个DOM元素
	  var div = document.createElement("div");
	  // 添加文字说明
	  div.appendChild(document.createTextNode("放大2级"));
	  // 设置样式
	  div.style.cursor = "pointer";
	  div.style.border = "2px solid gray";
	  div.style.backgroundColor = "white";
	  // 绑定事件,点击一次放大两级
	  div.onclick = function(e){
		map.setZoom(map.getZoom() + 2);
	  };
	  // 添加DOM元素到地图中
	  map.getContainer().appendChild(div);
	  // 将DOM元素返回
	  return div;
	};
	
	ZoomControl2.prototype.initialize = function(map){
	  // 创建一个DOM元素
	  var div = document.createElement("div");
	  // 添加文字说明
	  div.appendChild(document.createTextNode("缩小2级"));
	  // 设置样式
	  div.style.cursor = "pointer";
	  div.style.border = "2px solid gray";
	  div.style.backgroundColor = "white";
	  // 绑定事件,点击一次放大两级
	  div.onclick = function(e){
		map.setZoom(map.getZoom() - 2);
	  };
	  // 添加DOM元素到地图中
	  map.getContainer().appendChild(div);
	  // 将DOM元素返回
	  return div;
	};
	
	
	// 创建控件
	var myZoomCtrl = new ZoomControl();
	var myZoomCtrl2 = new ZoomControl2();
	// 添加到地图当中
	map.addControl(myZoomCtrl);
	map.addControl(myZoomCtrl2);
	
	
	
	
	
	

		function addPole(lng, lat, opts,color,bug,task) {
		var point = new BMap.Point(lng, lat);
		var myIcon = new BMap.Icon("images/circle"+color+".png", new BMap.Size(16, 16));

		var marker = new BMap.Marker(point, {
			icon : myIcon,
			enableDragging : false,
			draggingCursor : "move",
			title:task.id
		});
		
			var contextMenu = new BMap.ContextMenu();
	var saveMenu = new BMap.MenuItem("任务回执审批",function(e,p,type){
					auditing_receipt(task);
				},{width:80});
		contextMenu.addItem(saveMenu);
		marker.addContextMenu(contextMenu);
		
		/*	var contextMenu = new BMap.ContextMenu();
			contextMenu.addItem("删除杆塔", function() {
				alert("del");
			});
			marker.addContextMenu(contextMenu);
		 */
		marker.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			var  level=["一般","严重","紧急"];
			var html="<span style=\"font-size:12px;font-weight:bolder\">任务编号："+task.id+"&nbsp;&nbsp;任务名称："+task.name+"&nbsp;&nbsp;工作单据："+task.type+"</span><hr><span style=\"font-size:12px;font-weight:bolder\">线路名称:"+bug.routeName+"&nbsp;&nbsp;杆塔编号："+bug.pole.id+"&nbsp;&nbsp;缺陷等级："+level[bug.level-1]+"<br>发现人员："
					+ bug.userName + "&nbsp;&nbsp;发现时间:"+bug.time+"&nbsp;&nbsp;缺陷类型："+bug.typeName+"<br>缺陷描述："+bug.description+"</span>"
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y-5)
					.css("left", x+5).css("z-index", 1);
			routeDiv.html(html);
		});
		marker.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
		});
		
		
		map.addOverlay(marker);
		return marker;
	}
	
	function auditing_receipt(task){
	//receiptdebug
		var html ="<ul class='forminfo'>";
		html+="<li><label>任务编号<b>*</b></label><input  type='text' class='dfinput' value='"+task.id+"' disabled='disabled' /></li>";
		html+="<li><label>任务名称<b>*</b></label><input  type='text' class='dfinput' value='"+task.name+"' disabled='disabled' /></li>";
		html+="<li><label>完成情况描述<b>*</b></label><textarea id='description'  class='dfinput' style='width:345px;height:60px;' disabled='disabled'>"+task.receiptdebug.description+"</textarea></li>";
		html+="<li><label>间断延期记载<b></b></label><textarea id='defer'  class='dfinput' style='width:345px;height:60px;' disabled='disabled'>"+task.receiptdebug.defer+"</textarea></li>";
		html+="<li><label>工作终结报告<b></b></label><textarea id='bull'  class='dfinput' style='width:345px;height:60px;' disabled='disabled'>"+task.receiptdebug.bulletin+"</textarea></li>";
		html+="<li><label>任务审批意见<b></b></label><textarea id='idea'  class='dfinput' style='width:345px;height:60px;'></textarea></li>";
		html+="</ul>";
		art.dialog({title:"任务回执审批",content:html,button:[{value:"通过",focus:true,callback:function(){
			$.post("receiptDebug!auditingReceipt.action",{id:task.receiptdebug.id,idea:document.getElementById("idea").value,state:1},function(data){
				var json = eval("("+data+")");
				if(json.code==1){
					defaultAlert("任务回执审批","审批成功");
					setTimeout(function () { location.href="auditingDebugTask.html"; }, 2000);
				}else{
					defaultAlert("任务回执审批","审批失败");	
				}
			});
				return true;
			}
		},{value:"不通过",callback:function(){
				var idea = document.getElementById("idea");
				if(idea.value==""){
					idea.select();
					idea.focus();
					return false;
				}
				$.post("receiptDebug!auditingReceipt.action",{id:task.receiptdebug.id,idea:document.getElementById("idea").value,state:-1},function(data){
					var json = eval("("+data+")");
					if(json.code==1){
						defaultAlert("任务回执审批","审批成功");
						location.href="auditingDebugTask.html";
					}else{
						defaultAlert("任务回执审批","审批失败");	
					}
				});
					return true;	
			}
		},{value:"关闭",callback:function(){}
		}]});
	}
	
	
	
	
	var routeDiv;
	function addRoute(points, opts, color) {

		var polyline = new BMap.Polyline(points, {
			strokeColor : color,
			strokeWeight : 3,
			strokeOpacity : 0.8,
			enableClicking :true
		});
		
		polyline.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y)
					.css("left", x).css("z-index", 1);
			routeDiv.html("<span style=\"font-size:12px;font-weight:bolder\">"
					+ opts + "</span>");
					
		});
		polyline.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
			
		});
		
		
		map.addOverlay(polyline);
	}

		
	
	function calc_length(points) {
		var length=0;
			for(var m = 1;m<points.length;m++){
			console.log(length);
				length+=map.getDistance(points[m-1],points[m]);
			}
			
			
		return length;	
	}
	function getPointByPointTitle(title){
		var overlays = map.getOverlays();
			for ( var i = 0; i < overlays.length ; i++) {
			if (overlays[i]._config) {
				if (overlays[i]._config.title==title) {
					return overlays[i]._config.point;
				}
			}
		}
	}
	
	
	$.post("deBug!findMissPass.action", function(data) {
		var task = eval("("+data+")");
		var arr=["","","_red","_yellow"];
		for ( var i = 0; i < task.length; i++) {
			var points=[];
			var minLength=99999999;
			var minLine =[];
			for(var x=0;x<task[i].receiptpatrols.length;x++){
					points.push(new BMap.Point(task[i].receiptpatrols[x].pole.lng,task[i].receiptpatrols[x].pole.lat));
					addPole(task[i].receiptpatrols[x].pole.lng, task[i].receiptpatrols[x].pole.lat, task[i].receiptpatrols[x].pole.id,arr[task[i].receiptpatrols[x].level],task[i].receiptpatrols[x],task[i]);		
			}
			if(points.length>1){
				points = permutate(points);
				for(var m=0;m<points.length;m++){
					var len = calc_length(points[m]);
					if(len<minLength){
						minLine=points[m].concat();
						minLength=len;
					}
				}
				addRoute(minLine, task[i].id, "blue");
			}
			
		}
	});


</script>
